package com.iman.manjusri.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.common.support.Page;
import com.iman.manjusri.service.ArticlesService;
import com.iman.manjusri.vo.ArticlesVo;
import com.iman.manjusri.vo.CaseVo;
import com.iman.manjusri.vo.CourseAssistInfoVo;
import com.iman.manjusri.vo.CourseDetailVo;
import com.iman.manjusri.vo.CourseKeShiVo;
import com.iman.manjusri.vo.CourseRecommendVo;
import com.iman.manjusri.vo.CourseTeacherVo;

/**
 * 
 * @author Thinkpad
 *
 *德瑞姆网校资讯      通知公告      学院动态   行业动态   考试指南   备考指南    ，的页面
 *
 */ 

@Controller
@RequestMapping(value = "/article/*")
public class ArticlesController extends BaseController{
	
	@Autowired
	private ArticlesService articlesServiceImpl;
	
	@RequestMapping(value = "/{type}/{pcnt}/{page}")
	@ResponseBody()
	public Page  listArticles (
		 @Valid    @PathVariable  	String	type
		,@Valid    @PathVariable  	String	page 
		,@Valid    @PathVariable  	String	pcnt 
	  ){
		//分页的页数  如果没有的话 ，为0  第一页
		int pageInt = NumberUtils.toInt(page,0);
		//每页多少个
		int perPageInt = NumberUtils.toInt(pcnt,10);
		//类型
		Integer typeInt = NumberUtils.toInt(type,10);
		
		Map params = new HashMap();
		params.put("pageSize", perPageInt);
		params.put("pageIndex", pageInt);
		params.put("type", typeInt);
		
		//查询数据
		Page<ArticlesVo> pages = articlesServiceImpl.listArticles(params);
		
		
		
		
		return pages;
	}
	
	//查询分页显示案例分享的分页数据
	@RequestMapping(value = "/{pcnt}/{page}")
	@ResponseBody()
	public Page  listCaseByPage (
		 @Valid    @PathVariable  	String	type
		,@Valid    @PathVariable  	String	page 
		,@Valid    @PathVariable  	String	pcnt 
	  ){
		//分页的页数  如果没有的话 ，为0  第一页
		int pageInt = NumberUtils.toInt(page,0);
		//每页多少个
		int perPageInt = NumberUtils.toInt(pcnt,10);
		//类型
		//Integer typeInt = NumberUtils.toInt(type,10);
		
		Map params = new HashMap();
		params.put("pageSize", perPageInt);
		params.put("pageIndex", pageInt);
		//params.put("type", typeInt);
		
		//查询数据
		Page<CaseVo> pages = articlesServiceImpl.listCaseByPage(params);
		
		
		
		
		return pages;
	}
	
	
	  @RequestMapping(value = "/listTopNewCase")
	  public ModelAndView listTopNewCase(@NotNull @RequestParam(required = true)  String type,@NotNull @RequestParam(required = true)  String channelInfo){
		  Map<String,Object> result = new  HashMap<String,Object>();
		  ModelAndView view = null;
		  
		   //类型
			Integer typeInt = NumberUtils.toInt(type,10);
			
			Map params = new HashMap();
			params.put("type", typeInt);
			
			//查询数据
			Page<CaseVo> pages = articlesServiceImpl.listTopNewCase(params);
			Page<CaseVo> pagesHot = articlesServiceImpl.listTopHotCase(params);
		  
		  result.put("caseTopNew", pages.getData());
		  result.put("caseTopHot", pagesHot.getData());
		  
		  view = new ModelAndView("study_case",result);
		  putResourceSrc(view);	
		  return view;
	  }
	
	
	@RequestMapping(value = "/{articleId}")
	
	public String  getArticles (
		 @Valid    @PathVariable  	String  articleId,
		 HttpServletRequest request
	  ){
	/*	//分页的页数  如果没有的话 ，为0  第一页
		int pageInt = NumberUtils.toInt(page,0);
		//每页多少个
		int perPageInt = NumberUtils.toInt(pcnt,10);*/
		//类型
		System.out.println("*************articleId*********"+articleId);
		Map params = new HashMap();
		params.put("articleId", articleId);
		
		//查询数据
		Page<ArticlesVo> pages = articlesServiceImpl.getArticlesById(params);
		if(pages.getData().size()==0)
		{
			return "/kszn_son";
		}
		request.setAttribute("data", pages.getData().get(0));
		
		
		
		return "/kszn_son";
	}
	
	
	
	
}
